import React from 'react';
import {
  WeaDialog, WeaNewScroll, WeaBrowser, WeaUpload, WeaInput,
  WeaSearchGroup, WeaFormItem, WeaTableEdit, WeaAlertPage,WeaInputNumber,WeaShare
} from 'ecCom';
import { WeaSwitch } from 'comsMobx'
import MySwitch from './mySwitch.js'
import Decision from "./preview/decision/index.js"
import TabDiscuss from "./preview/tabDiscuss/index.js"
import TabSign from "./preview/tabsign/index.js"
import MeetService from "./preview/meetService/index.js"
import MeetMember from "./preview/meetMember/index.js"
import MeetTopic from "./preview/meetTopic/index.js"

//  会议共享
import MeetShare from './preview/meetShare/index.js'
//  排座信息
import ArrangeSeats from './preview/arrangeSeats/arrangeSeats';
//  打印席卡
import SeatCard from './preview/seatCard/index.js';

import { Tabs } from 'antd';
const TabPane = Tabs.TabPane;

import { inject, observer } from 'mobx-react'; 
import { observable, action, toJS } from 'mobx';
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
@inject('meetingDialog','meetingCalView','seatStore', 'seatCardStore')
@observer
class Preview extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      arrangeVisible: true
    }
  }
  componentWillMount(){
    let { setRender_start, setAll_start, setDialogState } = this.props.meetingDialog;
    setRender_start();
    setAll_start();
    setDialogState({meetingId: this.props.meetingId});
    // ////////////////////
    // const {getSeatInfo} = this.props.seatStore;
    // getSeatInfo({meetingId:  this.props.meetingId ,viewType: '2'});
  }
  render() {
    const { meetingId, seatStore, seatCardStore} = this.props
    let { dialogStatus, setDialogState, previewMeetingResultInfo } = this.props.meetingDialog;
    let { previewmeetingTab, jurisdiction, chatVisible, msginfo } = toJS(dialogStatus);
    const shareParams = {
      resourcetype:28,
      resourceid:meetingId
    }
    let TabPanes = [];
    if (jurisdiction[meetingId] == 'noright') {
      return (
        <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@esbedi`} style={{position: 'absolute', width:'100%', top:'40%'}}>
          <div>
            {getLabel(2012 ,'对不起，您暂时没有权限！')}
        </div>
        </WeaAlertPage>
      )
    } else if (jurisdiction[meetingId] == 'noSecretRight') {
      return (
        <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@bui56n`} style={{position: 'absolute', width:'100%', top:'40%'}}>
          <div>
            {getLabel(501082 ,'对不起，您的人员密级不足以查看该内容')}
        </div>
        </WeaAlertPage>
      )
    } else if (jurisdiction[meetingId] == 'noData') {
      return (
        <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@u6bca4`} style={{position: 'absolute', width:'100%', top:'40%'}}>
          <div>
            {getLabel(508093,'该会议已被删除！')}
        </div>
        </WeaAlertPage>
      )
    } else {
      TabPanes = this.getTabPanes();
    }
    
    return (
      <div className="meeting-previewMeeting" id="meeting-previewMeeting-id" style={{ height: '100%' }} ref='singleTab' isViewTab={previewMeetingResultInfo && previewMeetingResultInfo.isViewTab}>
        <Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@55g87i`} className="meeting-preview-tabs" activeKey={`${previewmeetingTab}`} onChange={(e) => { this.onChange(e) }} >
          {
            TabPanes.map((element, index) => {
              return element
            })
          }
        </Tabs>
        {seatStore.seatLayoutSettingVisible && <ArrangeSeats ecId={`${this && this.props && this.props.ecId || ''}_ArrangeSeats@kff62a`} meetingId={meetingId}/>}
        {seatCardStore.seatCardVisible && <SeatCard ecId={`${this && this.props && this.props.ecId || ''}_SeatCard@enmt13`} />}
        <WeaShare ecId={`${this && this.props && this.props.ecId || ''}_WeaShare@ww6qru`} shareParams={shareParams} visible = {chatVisible} msginfo={msginfo} onVisibleChange={bool => setDialogState({chatVisible:bool})}/>
      </div>
    )
  }

  onChangeSwitch = () => {
    // this.setState({a:'1'})
  }

  browserDemand = (oldFields, oldValue) => {
    let newFields = { ...oldFields};
    let newValue = { ...oldValue };
    let { isInterval } = this.props;
    const { ecCallerVisible } = this.props.meetingDialog;
    if (newFields.domkey[0] == 'meetingtype') {//会议类型-新建周期会议，在搜索时要多传个参数
      newFields.browserConditionParam.dataParams = { isInterval: '' }
      if (isInterval) newFields.browserConditionParam.dataParams = { isInterval: '1' }
    }
    if (ecCallerVisible && newFields.domkey[0] == 'caller') {//会议类型-召集人直接的联动，在搜索时要多传个参数
      newFields.browserConditionParam.dataParams = { meetingtype: newValue['meetingtype'] ? newValue['meetingtype'] : '' }
      newFields.browserConditionParam.completeParams = { meetingtype: newValue['meetingtype'] ? newValue['meetingtype'] : '' }
    }
    return newFields
  }
  
  getCondition = () => {
    let {meetingId} = this.props
    let {dialogStatus} = this.props.meetingDialog
    let {conditions} = toJS(dialogStatus)
    let key = meetingId
    if (conditions[key]) {
      return conditions[key]
    } else {
      return false
    }
  }
  
  getSearchs = () => {
    let { meetingId, meetingCalView, meetingDialog, dialogStatus } = this.props;
    let { form, getFormParams , getFormBrowserType,canRender} = meetingDialog;
    let isFormInit = false;
    if (form[meetingId]) {
      isFormInit = form[meetingId].isFormInit
    }
    const condition = this.getCondition()
    let value = getFormParams(meetingId);
    let group = [];
    isFormInit && canRender && value && condition && condition.length!=0 && condition.forEach((c, index) => {
      let items = []
      c && c.items && c.items.forEach(fields => {
        if (fields.conditionType == 'BROWSER') {
          fields = this.browserDemand(fields, value)
        }
		let key = "";
        if(fields.conditionType == 'RICHTEXT' && value.hasOwnProperty(fields.domkey[0])){
          key = fields.domkey[0];
        }
        if((!value['remindTypeNew'] || getFormBrowserType('remindTypeNew',meetingId) == 'INPUT')&&
          (fields.domkey[0] == 'remindImmediately' ||
            fields.domkey[0] == 'remindBeforeStart' ||
            fields.domkey[0] == 'remindBeforeEnd')) {
          return
        }else {
          if(fields.domkey[0] == 'remindBeforeStart' || fields.domkey[0] == 'remindBeforeEnd'){//提醒开始时间 提醒结束时间 上传组件单独处理
            items.push({
              com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@clz2k0@${fields.domkey[0]}`}
                label={`${fields.label}`}
                labelCol={{ span: `${fields.labelcol}`, offset: '2' }}
                wrapperCol={{ span: '11' }}
                underline
              >
                <MySwitch ecId={`${this && this.props && this.props.ecId || ''}_MySwitch@4ch629@${fields.domkey[0]}`} viewAttr={1} meetingType='preview' canset={false} fieldConfig={fields} form={form[meetingId]} onChange={this.onChangeSwitch} />
              </WeaFormItem>),
              colSpan: 1
            })
          }else {
            items.push({
              com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@pedfcs@${fields.domkey[0]}`}
                label={`${fields.label}`}
                labelCol={{ span: `${fields.labelcol}`, offset: '2' }}
                wrapperCol={{ span: '11' }}
                className={fields.domkey[0] != "address" ? '' : 'address'}
                underline
                tipPosition='bottom'
              >
                {fields.conditionType == 'RICHTEXT' ?
                  <div class="wea-field-readonly  " dangerouslySetInnerHTML = {{ __html: value[key]}}></div>
                   :
                   <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@908lj5@${fields.domkey[0]}`} fieldConfig={fields} form={form[meetingId]}/>
                }
                {fields.domkey[0] != "address" && fields.domkey[0] != "customizeAddress" ? '' :
                  <span style={{position: 'absolute', right: '-128px',bottom: '5px',maxWidth: '114px'}}>
                  <a
                     onClick={() => {
                       meetingCalView.isMeetingUsageBtn = false
                       this.props.setRoomPlan(true)
                     }}>{getLabel(15881,'会议室使用情况')}</a></span>}
              </WeaFormItem>),
              colSpan: 1,
              hide: form[meetingId].isHide(fields, (keys, allParams) => {
                let bool = false;
                //会议室使用情况 和 自定义会议室只显示有值的一个
                fields.domkey[0] === 'address' && value['address'] == '' && (bool = true);
                fields.domkey[0] === 'customizeAddress' && value['customizeAddress'] == '' && (bool = true);

                //重复模式repeatType相关联动
                fields.domkey[0] === 'repeatdays' && value['repeatType'] != 1 && (bool = true);
                fields.domkey[0] === 'repeatweeks' && value['repeatType'] !=2 && (bool = true);
                fields.domkey[0] === 'rptWeekDays' && value['repeatType'] !=2  && (bool = true);
                fields.domkey[0] === 'repeatmonths' && value['repeatType'] !=3 && (bool = true);
                fields.domkey[0] === 'repeatmonthdays' && value['repeatType'] !=3 && (bool = true);
                return bool;
              })
            })
          }
        }
      })
     group .push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@wjl5k3@${index}`} col={1} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
    });

    return group;
  }
  
  getTabPanes = () => {
    let { meetingId, isSingle ,isInterval, seatStore } = this.props;
    let { dialogStatus,weaDialogContentHeight,cusPreivewTab,meetingTabVisible } = this.props.meetingDialog;
    let { tabs, tabTopic, tabService, tabMember, conditions, previewmeetingTab } = toJS(dialogStatus);
    let oldTabArray = [];
    let styleHeight = isSingle ? '100%' : this.props.contentHeight-46 ;  //单页高度100%， 弹框454
    if (isSingle && this.refs.singleTab) {
      styleHeight = this.refs.singleTab.clientHeight - 52;
    }
    let oldTab = {
      'base': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@gse3th`} tab={getLabel(24249,'会议信息')} key="1" disabled={tabs ? (!tabs.base) : true}>
        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@1ansq4`} height={styleHeight}>
          <div className="testGetSearchs">
            {this.getSearchs()}
          </div>
        </WeaNewScroll>
      </TabPane>,
    };

    if (meetingTabVisible.some(v => v === 'topic')) {
      oldTab={
        ...oldTab,
        'topic': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@lvcho0`} tab={getLabel(31327,'会议议程')} key="2" disabled={tabs ? (!tabs.topic) : true} >
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@hycuds`} height={styleHeight}>
            <MeetTopic ecId={`${this && this.props && this.props.ecId || ''}_MeetTopic@vu28mx`} height={styleHeight} data={tabTopic} meetingId={meetingId} />
          </WeaNewScroll>
        </TabPane>,
      }
    }

    if (meetingTabVisible.some(v => v === 'service')) {
      oldTab={
        ...oldTab,
        'service': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@b41epo`} tab={getLabel(2107,'会议服务')} key="3" disabled={tabs ? (!tabs.service) : true} >
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@dcmpxx`} height={styleHeight}>
            <MeetService ecId={`${this && this.props && this.props.ecId || ''}_MeetService@ifflo9`} height={styleHeight} data={tabService} />
          </WeaNewScroll>
        </TabPane>,
      }
    }

    if (meetingTabVisible.some(v => v === 'meetingseat')) {
      oldTab={
        ...oldTab,
        'meetingseat': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@3fagrs`} tab={getLabel('506916','排座信息')} key="9">
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@p7ruqz`} height={styleHeight}>
            <div className="arrangeSeatInfo" style={{height: '100%'}}>
              {previewmeetingTab == '9' && !seatStore.seatLayoutSettingVisible && <ArrangeSeats ecId={`${this && this.props && this.props.ecId || ''}_ArrangeSeats@sb1qg5`} height={styleHeight} previewDisplay={true} meetingId={meetingId}/>}
            </div>
          </WeaNewScroll>
        </TabPane>,
      }
    }

    if(!isInterval){
      if (meetingTabVisible.some(v => v === 'member')) {
        oldTab={
          ...oldTab,
          'member': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@7vo6wi`} tab={getLabel(384782,'参会回执')} key="4" disabled={tabs ? (!tabs.member) : true} >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@j63w1m`} height={styleHeight}>
              <MeetMember ecId={`${this && this.props && this.props.ecId || ''}_MeetMember@0hjy76`} data={tabMember} meetingId={meetingId} />
            </WeaNewScroll>
          </TabPane>,
        }
      }
      
      if (meetingTabVisible.some(v => v === 'decision')) {
        oldTab={
          ...oldTab,
          'decision': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@4jrt4h`} tab={getLabel(2194,'会议决议')} key="5" disabled={tabs ? (!tabs.decision) : true} >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@6w2a7r`} height={styleHeight}>
              <Decision ecId={`${this && this.props && this.props.ecId || ''}_Decision@2g6o3p`}/>
            </WeaNewScroll>
          </TabPane>,
        }
      }

      if (meetingTabVisible.some(v => v === 'sign')) {
        oldTab={
          ...oldTab,
          'sign': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@8s6msr`} tab={getLabel(383592,'会议签到')} key="6" disabled={tabs ? (!tabs.sign) : true} >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@xo1e7z`} height={styleHeight}>
              {previewmeetingTab == '6' && <TabSign ecId={`${this && this.props && this.props.ecId || ''}_TabSign@a7i5wt`}
                meetingId={meetingId}
              />}
            </WeaNewScroll>
          </TabPane>,
        }
      }
      if (meetingTabVisible.some(v => v === 'discuss')) {
        oldTab={
          ...oldTab,
          'discuss': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@41mz89`} tab={getLabel(15153,'相关交流')} key="7" disabled={tabs ? (!tabs.discuss) : true} >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@vjiyde`} height={styleHeight}>
              <TabDiscuss ecId={`${this && this.props && this.props.ecId || ''}_TabDiscuss@3h357z`}
                meetingid={meetingId}
              />
            </WeaNewScroll>
          </TabPane>,
        }
      }
      if (meetingTabVisible.some(v => v === 'share')) {
        oldTab={
          ...oldTab,
          'share': <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@y3h5ug`} tab={getLabel(82752,'共享范围')} key="8" disabled={tabs ? (!tabs.share) : true} >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@ul6d27`} height={styleHeight}>
              <MeetShare ecId={`${this && this.props && this.props.ecId || ''}_MeetShare@c3bnmg`} meetingId={meetingId}/>
            </WeaNewScroll>
          </TabPane>,
        }
      }

    }

    for (let i in oldTab) {
      if((i == 'meetingseat')) {
        (tabs[i] || seatStore.isSeatTabstatus) && oldTabArray.push(oldTab[i])
      } else {
        tabs[i] && oldTabArray.push(oldTab[i])
      }
    }
    //普通会员增加页签显示
    if(!isInterval){
      cusPreivewTab=toJS(cusPreivewTab);
      for(let i in cusPreivewTab){
        oldTabArray.push(<TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@w8c1z5@customTab_${i}`} tab={cusPreivewTab[i].title} key={i} >
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@5bt4w1@customTabScroll_${i}`} height={styleHeight}>
            <iframe meetingid={meetingId} src={cusPreivewTab[i].url+meetingId} width="100%" height="100%" />
          </WeaNewScroll>
        </TabPane>)
      }
    }
   
    return oldTabArray;
  }
  
  onChange = (e) => {
    let { dialogStatus, changePreviewMeetingTab, getTabSignTable, getTabDecisionTable } = this.props.meetingDialog;
    const {getBaseSetDatas} = this.props.seatStore;
    let { tabSign, tabDecision } = toJS(dialogStatus);
    changePreviewMeetingTab(e);
    if (e == '6') {
      setTimeout(() => { getTabSignTable(tabSign) }, 800)
    }
    if (e == "5") {
      setTimeout(() => { getTabDecisionTable(tabDecision.list.sessionkey) }, 800)
    }
    if (e == "9") {
      this.setState({arrangeVisible: true})
    }else{
      this.setState({arrangeVisible: false})

    }
  }
}

export default Preview
